//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

// TODO: Add logic to ensure only users in the Forum-Moderators group have access

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Discussions.Components;

namespace CommunityServer.Discussions.Controls 
{
    // *********************************************************************
    //  QuickDeletePost
    //
    /// <summary>
    /// This control is used by forum moderators to delete posts
    /// </summary>
    // ***********************************************************************/
    public class QuickDeletePost : TemplatedWebControl
    {
        #region Child Controls

        CSContext csContext;
        RequiredFieldValidator ValidateReason;
        TextBox reasonForDelete;
        Label hasReplies;
        ForumPost postToDelete;
        Button deleteButton;
        Button cancelButton;
        
        #endregion

        protected override void OnInit(EventArgs e) 
        {
            csContext = CSContext.Current;

            if (SkinName == null)                
                ExternalSkinFileName = "DeletePost.ascx";
            else 
                ExternalSkinFileName = SkinName;

            // Get the post we are deleting
            postToDelete = Posts.GetPost(csContext.PostID, csContext.User.UserID);

            // Check if the user has permission to delete the post
            //
            Permissions.AccessCheck(postToDelete.Section, Permission.Delete, csContext.User, postToDelete);

            base.OnInit(e);
        }

        #region Skin

        protected override void AttachChildControls() 
        {
            // Text box containing the reason why the post was deleted. This note will be
            // sent to the end user.
            reasonForDelete = (TextBox) FindControl("DeleteReason");
			reasonForDelete.Text = string.Format("{0}\n{1}", ResourceManager.GetString("DeletePost_RejectedMessage"), postToDelete.Body);

            // Display the post being deleted
            //
            ((Label) FindControl("PreviewSubject")).Text = string.Format(ResourceManager.GetString("DeletePost_Title"), postToDelete.Subject);
			((Label) FindControl("PreviewBody")).Text = postToDelete.FormattedBody;

            // Who is deleting the post
            //
			((Label)FindControl("DeletedBy")).Text = CSContext.Current.User.DisplayName;

            // Does the post have any replies?
            hasReplies = (Label) FindControl("HasReplies");
            if (null != hasReplies) 
            {
                // check to see if the Moderator is deleting childposts
                //
                if (csContext.QueryText == "childposts") 
                {
                    if (postToDelete.Replies > 0)
                        hasReplies.Text = ResourceManager.GetString("Yes") + " (" + postToDelete.Replies.ToString() + ") ";
                    else
                        hasReplies.Text = ResourceManager.GetString("No");
                } 
                else
                    hasReplies.Text = ResourceManager.GetString("DeletePost_NA");
            }

            // Perform the delete
            deleteButton = (Button) FindControl("DeletePost");
            if (null != deleteButton) 
            {
                deleteButton.Attributes["onclick"] = "return confirm('" + ResourceManager.GetString("DeletePost_PopupConfirmation").Replace("'", @"\'") + "');";								
                deleteButton.Text = ResourceManager.GetString("DeletePost_DeletePost");
            }

            // Cancel the delete
            cancelButton = (Button) FindControl("CancelDelete");
            if (null != cancelButton) 
                cancelButton.Text = ResourceManager.GetString("DeletePost_CancelDelete");

            // Validator for reason to delete
            ValidateReason = (RequiredFieldValidator) FindControl("ValidateReason");
            ValidateReason.ErrorMessage = ResourceManager.GetString("DeletePost_ValidateReason");

            InitializeChildControls();
        }

        private void InitializeChildControls() 
        {
            if (null != deleteButton) 
                deleteButton.Click += new System.EventHandler(DeletePost_Click);

            if (null != cancelButton) 
                cancelButton.Click += new EventHandler(CancelDelete_Click);
        }

        #endregion

        #region Event Handlers
        
        // *********************************************************************
        //  DeletePost_Click
        //
        /// <summary>
        /// Event handler for deleting a post
        /// </summary>
        // ***********************************************************************/
        private void DeletePost_Click(Object sender, EventArgs e) 
        {
            // Are we valid?
            if (ValidateReason.IsValid) 
            {
                // Get the post we are going to delete
                //
                ForumPost post = Posts.GetPost(csContext.PostID, csContext.User.UserID);


                // Perform the delete
                //
                if (csContext.QueryText == "childposts")
                    Moderate.DeletePost(post, csContext.User, reasonForDelete.Text, true);
                else
                    Moderate.DeletePost(post, csContext.User, reasonForDelete.Text, false);

				Modal.ClosePage(this.Page, this.postToDelete.PostID.ToString());
            }

        }

        // *********************************************************************
        //  CancelDelete_Click
        //
        /// <summary>
        /// Event handler for canceling deletion of a post
        /// </summary>
        // ***********************************************************************/
        private void CancelDelete_Click(Object sender, EventArgs e) 
        {
			Modal.ClosePage(this.Page, "-1");
        }

        #endregion
    }
}
